package edu.lambdateacher.parser;

import edu.lambdateacher.PropertiesManager;


/*
 LambdaTeacher
 Copyright (C) 2000  Kim Mason
 Copyright (C) 2014  Vitalij Zadneprovskij

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

/**
 * The representation of a Lambda token, used by the Lambda scanner. At the
 * moment, it handles PURE Lambda tokens, integers and +,-,*,/
 */
public class LambdaToken {
	// holds identifier value, if token_type = IDENT
	private String identifierName; 
	private LambdaTokenType tokenType;
	private int numValue;
	// which line number and position did the token
	// occur on
	private int lineNumber; 
							
	private int linePosition;

	/** Constructor to create a standard token of any type */
	public LambdaToken(LambdaTokenType intokenType) {
		this.tokenType = intokenType;
		this.identifierName = "";
		this.numValue = 0;
	}

	/** Constructor to create an identifier */
	public LambdaToken(LambdaTokenType intoken_type, String inidentName) {
		this.tokenType = intoken_type;
		this.identifierName = inidentName;
		this.numValue = 0;
	}

	/** Initialiser to create a number */
	public LambdaToken(LambdaTokenType intokenType, int innumber) {
		this.tokenType = intokenType;
		this.identifierName = "";
		this.numValue = innumber;
	}

	public void setLineAndPosition(int lineNumber, int linePos) {
		this.lineNumber = lineNumber;
		this.linePosition = linePos;
	}

	public void setlineno(int lineNumber) {
		this.lineNumber = lineNumber;
	}

	public void setlinepos(int linePos) {
		this.linePosition = linePos;
	}

	/**
	 * A useful method for debugging. Prints out the type, value, line number
	 * and position of the LambdaToken.
	 */
	public String print() {
		String toreturn;
		
		if (tokenType == LambdaTokenType.IDENT){
			toreturn = "Identifier:  " + identifierName;
		}else if (tokenType == LambdaTokenType.LAMBDA){
			toreturn = "Lambda";
		}else if (tokenType == LambdaTokenType.PERIOD){
			toreturn = "Period";
		}else if (tokenType == LambdaTokenType.LPAREN){
			toreturn = "Left bracket";
		}else if (tokenType == LambdaTokenType.RPAREN){
			toreturn = "Right bracket";
		}else if (tokenType == LambdaTokenType.LET){
			toreturn = "Let";
		}else if (tokenType == LambdaTokenType.EQUALS){
			toreturn = "Equals";
		}else if (tokenType == LambdaTokenType.PLUS){
			toreturn = "Plus";
		}else if (tokenType == LambdaTokenType.MINUS){
			toreturn = "Minus";
		}else if (tokenType == LambdaTokenType.STAR){
			toreturn = "Star";
		}else if (tokenType == LambdaTokenType.SLASH){
			toreturn = "Slash";
		}else if (tokenType == LambdaTokenType.NUMBER){
			toreturn = "Number: " + String.valueOf(numValue);
		}else if (tokenType == LambdaTokenType.EOF){
			toreturn = "End of file";
		}else if (tokenType == LambdaTokenType.UNKNOWN){
			toreturn = "Unknown";
		}else{
			toreturn = "";
		}

		toreturn = toreturn + "    ";
		toreturn = toreturn + "Line: " + Integer.toString(lineNumber);
		toreturn = toreturn + "    ";
		toreturn = toreturn + "Posn: " + Integer.toString(linePosition);

		return toreturn;
	}

	/**
	 * The toString method takes in an type of LambdaToken, and returns a String
	 * with that types name. e.g. takes in 5, spits out Identifier
	 */
	public String toString() {
		if (tokenType != null) {
			switch (tokenType) {
			case LAMBDA:
				return PropertiesManager.LAMBDA_STRING;
			case PERIOD:
				return "\".\"";
			case LPAREN:
				return "\"(\"";
			case RPAREN:
				return "\")\"";
			case IDENT:
				return "Identifier: " + identifierName;
			case LET:
				return "\"Let\"";
			case EQUALS:
				return "\"=\"";
			case PLUS:
				return "\"+\"";
			case MINUS:
				return "\"-\"";
			case STAR:
				return "\"*\"";
			case SLASH:
				return "\"/\"";
			case NUMBER:
				return "Number";
			case EOF:
				return "EOF";
			case UNKNOWN:
				return "Unknown";
			default:
				break;
			}
		}
		return "LambdaToken [ident_name=" + identifierName + ", token_type=" + tokenType + ", num_value=" + numValue + ", line_number=" + lineNumber + ", line_position=" + linePosition + "]";
	}

	public String getIdentifierName() {
		return identifierName;
	}

	public void setIdentifierName(String identifierName) {
		this.identifierName = identifierName;
	}

	public LambdaTokenType getTokenType() {
		return tokenType;
	}

	public void setTokenType(LambdaTokenType tokenType) {
		this.tokenType = tokenType;
	}

	public int getNumValue() {
		return numValue;
	}

	public void setNumValue(int num_value) {
		this.numValue = num_value;
	}

	public int getLineNumber() {
		return lineNumber;
	}

	public void setLineNumber(int lineNumber) {
		this.lineNumber = lineNumber;
	}

	public int getLinePosition() {
		return linePosition;
	}

	public void setLinePosition(int linePosition) {
		this.linePosition = linePosition;
	}

}
